/*
 * @(#)FSEMExample.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing;

import org.eoti.awt.ColorUtil;
import org.eoti.awt.CursorUtil;
import org.eoti.awt.GraphicsUtil;
import org.eoti.awt.WebColor;
import static org.eoti.util.TimeUtil.Second;

import javax.swing.*;
import java.awt.event.*;
import static java.awt.event.KeyEvent.VK_ENTER;
import java.awt.*;
import java.util.*;

public class FSEMExample
extends FSEMFrame
implements MouseListener, MouseMotionListener, MouseWheelListener, KeyListener
{
	protected static Random rand;
	protected static Color textColor, pointColor, backColor;
	protected static Font closeFont, fpsFont;

	public static void main(String[] args)
	{
		if(args.length != 1)
		{
			System.out.println("USAGE: java org.eoti.swing.FSEMExample requestFramesPerSecond");
			System.exit(0);
		}

		rand = new Random();
		backColor = WebColor.CornSilk.getColor();
		textColor = Color.blue;
		pointColor = Color.black;
		closeFont = new Font("SansSerif", Font.PLAIN, 40);
		fpsFont = new Font("SansSerif", Font.BOLD, 12);

		try{
			int reqFPS = Integer.parseInt(args[0]);
			/**
			 * setDefaultLookAndFeelDecorated(true) causes the titlebar to
			 * not show up in Windowed mode
			 *
			 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050922
			 */
//		    	JFrame.setDefaultLookAndFeelDecorated(true);
			FSEMExample example = new FSEMExample(reqFPS);
		}catch(Exception e){
			System.out.format("Exception: %s", e.getMessage());
			e.printStackTrace();
		}
	}

	public FSEMExample(int reqFPS)
	{
		super("FSEMExample", reqFPS);
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		this.addMouseWheelListener(this);
		this.addKeyListener(this);
		this.getTimer().printStats(true);

		this.setCursor(CursorUtil.CursorType.None); //Crosshair
/*
		try{
			this.setDisplayMode(800, 600, 32);
		}catch(Exception e){
			System.out.format("SET-DISPLAY: Exception: %s", e.getMessage());
			e.printStackTrace();
		}
*/
	}

	protected int currX = 0;
	protected int currY = 0;
	protected String fpsString = "";
	protected String closeString = "Click to Close";

	public void activeUpdate()
	{
		textColor = randomColor();
//			pointColor = randomColor();
		fpsString = String.format(
			"%03.2f/%03.2f",
			getTimer().getAverageFPS(),
			getTimer().getAverageUPS()
		);
	}

	public void activeRender(Graphics g)
	{
		GraphicsUtil gu = new GraphicsUtil(g, this);
		Graphics2D g2 = (Graphics2D)g;
		Rectangle bounds = this.getBounds();

		g2.setColor(backColor);
		g2.fill(bounds);

		g2.setColor(textColor);
		g2.setFont(closeFont);
		gu.drawString(closeString, bounds, GraphicsUtil.Align.Center);
		g2.setColor(pointColor);
		g2.drawLine((int)bounds.getMinX(), (int)bounds.getMinY(), currX, currY);
		g2.drawLine((int)bounds.getMaxX(), (int)bounds.getMinY(), currX, currY);
		g2.drawLine((int)bounds.getMinX(), (int)bounds.getMaxY(), currX, currY);
		g2.drawLine((int)bounds.getMaxX(), (int)bounds.getMaxY(), currX, currY);
		//g2.fillOval(currX, currY, 10, 10);

		g2.setColor(Color.black);
		g2.setFont(fpsFont);
		gu.drawString(fpsString, bounds, GraphicsUtil.Align.NorthEast);
	}

	public Color randomColor()
	{
		int r = rand.nextInt(WebColor.values().length);
		return (WebColor.values()[r]).getColor();
	}

	public void mouseClicked(MouseEvent event)
	{
		if(SwingUtilities.isRightMouseButton(event))
		{
			toggleFullScreen();
		}else if(SwingUtilities.isLeftMouseButton(event)){
//			if(SwingUtilities.isLeftMouseButton(event)){
			setFullScreen(false);
			Second.sleep(1);
			System.exit(0);
		}
	}

	public void mouseEntered(MouseEvent event)
	{
	}

	public void mouseExited(MouseEvent event)
	{
	}

	public void mousePressed(MouseEvent event)
	{
	}

	public void mouseReleased(MouseEvent event)
	{
	}

	public void mouseDragged(MouseEvent event)
	{
	}

	public void mouseMoved(MouseEvent event)
	{
		currX = event.getX();
		currY = event.getY();
	}

	public void mouseWheelMoved(MouseWheelEvent event)
	{
	}

	public void keyPressed(KeyEvent event)
	{
	}

	public void keyReleased(KeyEvent event)
	{
		if(event.isAltDown() && (event.getKeyCode() == VK_ENTER))
			toggleFullScreen();
	}

	public void keyTyped(KeyEvent event)
	{
		// ALT-ENTER doesn't show up in keyTyped, but it DOES in keyReleased
		// maybe switch this to use InputMap instead?
		/*
		System.out.println("event.isAltDown? " + event.isAltDown());
		System.out.println("event.getKeyCode(): " + event.getKeyCode());
		System.out.println("VK_ENTER: " + VK_ENTER);
		if(event.isAltDown() && (event.getKeyCode() == VK_ENTER))
			toggleFullScreen();
		*/
	}
}
